<?php

class KehoachdauthauController extends BackEndController {


    /**
     * This is the default 'index' action that is invoked
     * when an action is not explicitly requested by users.
     */
    public function actionIndex() {
        $model = new KeHoachDauThauForm();
        $model->unsetAttributes(); // clear any default values
        $model->type = FormData::KE_HOACH_DAU_THAU;
        if (isset($_GET['KeHoachDauThauForm'])) {
            $model->attributes = $_GET['KeHoachDauThauForm'];
        }
        $this->render('index', array(
            'model' => $model,
        ));
        
    }

    public function actionUpdate($id) {
        Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl . '/plugins/ckeditor/ckeditor.js', CClientScript::POS_END);
        $model = new KeHoachDauThauForm();
        $model->loadById($id);
        if (isset($_POST['KeHoachDauThauForm'])) {
            $model->attributes = $_POST['KeHoachDauThauForm'];
            $model->user_update = Yii::app()->user->id;
            $model->updated_at = date('Y-m-d H:i:s');
            $model->is_checked = $_POST['KeHoachDauThauForm']['is_checked'];
            $model->user_check = Yii::app()->user->id;
            $model->checked_at = date('Y-m-d H:i:s');
            $uploadedFile = CUploadedFile::getInstance($model, 'image_url');

            if ($uploadedFile !== null) {
                $uploadedFile->saveAs(
                        Yii::getPathOfAlias('webroot') . '/upload/' . $uploadedFile->name);
               $model->image_url = '/upload/' . $uploadedFile->name;
           }
            if ($model->validate() && $model->save()) {
                Yii::app()->user->setFlash('success', 'Cập nhật thành công.');
                $this->refresh();
            } else {
                Yii::app()->user->setFlash('error', 'Đã có lỗi xảy ra. Vui lòng kiểm tra lại.');
            }
        }
        $this->render('update', array('model' => $model));
    }

}
